package com.h2cloud.springCondition;

/**
 * @Package: com.h2cloud.springCondition
 * @ClassName: LinuxCondition
 * @Author: h2cloud
 * @CreateTime: 2023/11/15 12:04 上午
 * @Description:
 */

import lombok.Data;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 实现spring 的Condition接口，并且重写matches()方法，如果@ConditionalOnLinux的注解属性environment是linux就返回true
 *
 */
public class LinuxCondition {
	public static void main(String[] args) {
		// ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		// executor.setCorePoolSize(15);
		// executor.setMaxPoolSize(30);
		// executor.setQueueCapacity(1024);
		// executor.setThreadNamePrefix("rollingForecastTaskExecutor-");
		// executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		// executor.initialize();
		//
		// List<Integer> subList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 0));
		//
		// long startTime = System.currentTimeMillis();
		// for (int i = 0; i < 10; i++) {
		// 	int total = 100;
		// 	List<Integer> resList = new CopyOnWriteArrayList<>();
		// 	CountDownLatch countDownLatch = new CountDownLatch(total);
		// 	for (int j = 0; j < total; j++) {
		// 		executor.submit(() -> {
		// 			long l = System.currentTimeMillis();
		// 			resList.addAll(subList);
		// 			System.out.println("addAll cost: " + (System.currentTimeMillis() - l));
		// 			countDownLatch.countDown();
		// 		});
		// 	}
		//
		// 	try {
		// 		countDownLatch.await();
		// 	} catch (Exception e) {
		// 		e.printStackTrace();
		// 	}
		// 	if (resList.size() != total * 10) {
		// 		System.out.println("end != " + resList.size());
		// 	} else {
		// 		System.out.println("end == " + resList.size());
		// 	}
		// }
		// System.out.println("end ..... cost: " + (System.currentTimeMillis() - startTime));


		long l = System.currentTimeMillis();
		List<position> list = new ArrayList<>();
		for (int i = 0; i < 60000; i++) {
			position position = new position();
			position.setId(1);
			position.setLabelId(1);
			position.setVersion(1);
			list.add(position);
		}
		System.out.println(System.currentTimeMillis() - l);
	}
}

@Data
class position {
	Integer id;
	Integer labelId;
	Integer num;
	Integer version;
}
